//
// Ephi - simulation of magnetic fields and particles
// Copyright (C) 2007 Indrek Mandre <indrek(at)mare.ee>
// For more information please see http://www.mare.ee/indrek/ephi/
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#include "ephi.hpp"

int main (int argc, char *argv[])
{
  {
  Statics statics;
  ElectroDynamics dyn(statics);
  make_polywell_cube (statics, 0.3, 0.02, 0.005, 100000);
  fprintf (stderr, "Calculating the cube configuration...\n");
  Scene scene(dyn);
  Screen screen(800, 800, 1.0, 1.0);
  scene.calc (screen);
  scene.render_map (screen);
  screen.write ("polywell_cube_bfield.bmp");
  printf ("OK: polywell_cube_bfield.bmp written..\n");
  scene.set_coloring (32, true);
  scene.render_map (screen);
  screen.write ("polywell_cube_bfield_log.bmp");
  printf ("OK: polywell_cube_bfield_log.bmp written..\n");
  scene.set_coloring (1); // B&W
  scene.render_lic (screen);
  screen.write ("polywell_cube_bfield_lic.bmp");
  scene.set_coloring (0, true); // logarithmic full scale
  scene.render_lic (screen);
  screen.write ("polywell_cube_bfield_lic_logcolor.bmp");
  }

  {
  Statics statics;
  ElectroDynamics dyn(statics);
  make_polywell_dodecahedron (statics, 0.3, 0.02, 0.005, 100000);
  fprintf (stderr, "Calculating the dodecahedron configuration...\n");
  Screen screen(800, 800, 1.5, 1.5);
  Scene scene(dyn);
  scene.calc (screen);
  scene.render_map (screen);
  screen.write ("polywell_dodec_bfield.bmp");
  printf ("OK: polywell_dodec_bfield.bmp written..\n");
  scene.set_coloring (32, true);
  scene.render_map (screen);
  screen.write ("polywell_dodec_bfield_log.bmp");
  printf ("OK: polywell_dodec_bfield_log.bmp written..\n");
  }

  {
  FILE *fp = fopen ("polywell_cube.csv", "w");
  if ( !fp )
      fp = stdout;

  fprintf (fp, "R/S");
  for ( int j = 0; j <= 30; j++ )
    {
      prec_t A = j * 0.01;
      fprintf (fp, ",%g", prec2double(A));
    }
  fprintf (fp, "\n");

  for ( int i = 0; i <= 100; i++ )
    {
      fprintf (stderr, "Calculating %d/%d\n", i, 100);
      prec_t R = 0.05 + i * 0.025;
      fprintf (fp, "%g", prec2double(R));
      for ( int j = 0; j <= 30; j++ )
        {
          Statics statics;
          prec_t A = j * 0.01;
          prec_t RR = R + A / PREC_SQRT2;
          make_polywell_cube (statics, R, 0, A, 1);
          vect3d bf, ef;
          statics.getFields (vect3d(-RR, 0, 0), bf, ef);
          fprintf (fp, ",%g", prec2double(bf.length()));
        }
      fprintf (fp, "\n");
      fflush (fp);
    }
  if ( fp != stdout )
      fclose (fp);
  printf ("OK: written polywell_cube.csv with bfield estimates\n");
  }
  return 0;
}

